package cc.javaee.bbs.service.awardInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.javaee.bbs.dao.awardInfo.AwardInfoDao;
import cc.javaee.bbs.model.ActiveAward;
import cc.javaee.bbs.model.AwardInfo;
import cc.javaee.bbs.model.PageBean;
/**
 * 奖品service
 * @author admin
 *
 */
@Service
@Transactional
public class AwardInfoService {
	@Autowired
	private  AwardInfoDao awardInfoDao;
	public PageBean<AwardInfo> findpage(AwardInfo awardInfo,PageBean<AwardInfo> page) {
		page.setBean(awardInfo);
		int count = awardInfoDao.findpagecount(page);
		page.setTotalRecords(count);
		List<AwardInfo> list = awardInfoDao.findpage(page);
		page.setList(list);
		return page ;
	}
	public AwardInfo findbyid(AwardInfo awardInfo) {
		List<AwardInfo> list= awardInfoDao.findbyid(awardInfo);
		if(list.size()>0){
			awardInfo=list.get(0);
		}
		return awardInfo;
	}
	public void insert(AwardInfo awardInfo) {
		awardInfo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		awardInfoDao.insert(awardInfo);
	}

	public String delete(Integer id) {
		String result="1";
		//查询此奖品有没有关联的活动
		List<ActiveAward>activeAwards=awardInfoDao.findActiveAwards(id);
		if(activeAwards.size()>0){
			result="2";
		}else{
			awardInfoDao.delete(id);
		}
		return result;
	}
	public void update(AwardInfo awardInfo) {
		awardInfo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		awardInfoDao.update(awardInfo);
	}
	/**
	 * App接口根据活动id查询奖品
	 * @param activeId  活动ID
	 * @return
	 */
	public List<AwardInfo>findOrderInfoByActiveId(String activeId){
		List<AwardInfo>list=awardInfoDao.findOrderInfoByActiveId(activeId);
		return list;
	}

}
